package com.rjwh.dingdong.client.activity.more;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.loginregist.ThirdPartyLoginActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.mvp.model.impl.MoreSettingModel;
import com.rjwh.dingdong.client.mvp.presenter.impl.MoreSettingPresenter;
import com.rjwh.dingdong.client.mvp.view.MoreSettingView;
import com.rjwh.dingdong.client.util.RxBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName MoreSettingActivity
 * @Description
 * @date 2016-10-18
 * @history 2016-10-18 author: description:
 */
public class MoreSettingActivity extends BaseActivity implements MoreSettingView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private MoreSettingPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_setting_layout);
        ButterKnife.bind(this);
        initToolbarHelper();
        presenter = new MoreSettingPresenter(this,this);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(getResources().getString(R.string.more_setting));
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @OnClick({R.id.more_setting_personal_info_root, R.id.more_setting_bind_social_account_root, R.id.more_setting_account_security_root, R.id.more_setting_login_out_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_setting_personal_info_root:
                startActivity(new Intent(this, MorePersonDataActivity.class));
                break;
            case R.id.more_setting_bind_social_account_root:
                bindSocialAccount();
                break;
            case R.id.more_setting_account_security_root:
                startActivity(new Intent(this, MoreAccountSecurityActivity.class));
                break;
            case R.id.more_setting_login_out_btn:
                doLogout();
                break;
        }
    }

    /**
     * 绑定社交账号
     */
    private void bindSocialAccount() {
        Intent intent = new Intent(this, MoreBindSocialAccountActivity.class);
        startActivity(intent);
    }

    /**
     * 退出登录
     */
    private void doLogout() {
        presenter.doSysLogout();
        MyApplication.spUtil.setBoolPreference(MyApplication.app, LocalConstant.SP_IS_LOGINED, false);
        MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_BABYID, "0");
        MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_USERID, "0");
        //退出登录后更新个人中心视图
        RxBus.get().post(LocalConstant.RX__POST_LOGIN_UPDATE_UI, 2);
        Intent intent = new Intent(this, ThirdPartyLoginActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
